package org.winplus.serial;

import java.awt.font.TextAttribute;
import java.io.IOException;
import java.security.InvalidParameterException;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import org.winplus.serial.utils.SerialPortFinder;

public class SerialDemoActivity extends SerialPortActivity{
    private static final String TAG = "SerialDemoActivity";
	EditText mReception;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            getSerialport();

            try {
                mSerialPort = mApplication.getSerialPort();
                mOutputStream = mSerialPort.getOutputStream();
                mInputStream = mSerialPort.getInputStream();

                /* Create a receiving thread */
                mReadThread = new ReadThread();
                mReadThread.start();
            } catch (SecurityException e) {
                    DisplayError(R.string.error_security);
            } catch (IOException e) {
                    DisplayError(R.string.error_unknown);
            } catch (InvalidParameterException e) {
                    DisplayError(R.string.error_configuration);
            }

            mReception = (EditText) findViewById(R.id.EditTextReception);
            //send
            EditText Emission = (EditText) findViewById(R.id.EditTextEmission);
            Emission.setOnEditorActionListener(new OnEditorActionListener() {
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        Log.d(TAG,"onEditorAction");
                        int i;
                        CharSequence t = v.getText();
                        char[] text = new char[t.length()];
                        for (i=0; i<t.length(); i++) {
                                text[i] = t.charAt(i);
                        }
                        try {
                                mOutputStream.write(new String(text).getBytes());
                                mOutputStream.write('\n');
                            Log.d(TAG,"send: "+text);
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                        return false;
                    }
            });
    }

    private void getSerialport() {
        SerialPortFinder serialPortFinder = new SerialPortFinder();
        serialPortFinder.getAllDevices();
        serialPortFinder.getAllDevicesPath();

    }

    @Override
    protected void onDataReceived(final byte[] buffer, final int size) {
            runOnUiThread(new Runnable() {
                    public void run() {
                        Log.d(TAG,"data received.");
                        if (mReception != null) {
                                mReception.append(new String(buffer, 0, size));
                        }
                    }
            });
    }
}